package com.comp5900.impl;

import com.comp5900.impl.huffman.*;
import com.comp5900.impl.lcs.*;
import com.comp5900.util.HuffmanUtility;

import java.io.*;
import java.util.ArrayList;

public class FileTest {

	public static void main(String[] args){
		String path = "D:\\projects\\workspace\\copy detection\\src\\com\\comp5900\\impl\\lcs\\LongestCommonSubsequenceFinder.java";
		FileInputStream input = null;
		ArrayList<SymbolFrequencyPair<Byte>> map = new ArrayList<SymbolFrequencyPair<Byte>>();
		byte[] frequency;
		String original = "0000001001111101001011000111100101110001000010111101100010111010010100111011000000110001101101011110110001011101";
		
		try {
			input = new FileInputStream(path);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

			return;
		}

		frequency = new byte[256];

		try {
			while( input.available() > 0 ){
				byte b = (byte)input.read();
				frequency[b]++;
			}

			input.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for( int i=0; i<frequency.length; i++)
			if( frequency[i] > 0)
				map.add( new SymbolFrequencyPair<Byte>(Byte.valueOf((byte)i), frequency[i]) );

		HuffmanTree tree = HuffmanEncoder.buildEncodingTree(map);

		String result = HuffmanUtility.getInorderTraversalOfEncodingTree(tree);

		ArrayList<Byte> a = new ArrayList<Byte>(), b = new ArrayList<Byte>();

		for( char c : original.toCharArray() )
			a.add(Byte.valueOf( (byte)c ));

		for( char c : result.toCharArray() )
			b.add(Byte.valueOf( (byte)c ));

		int length = LongestCommonSubsequenceFinder.computeLongestCommonSubsequence( a.toArray(), b.toArray() ).size();

		System.out.println("Percent of match: " + ( (float)length / original.length() )*100 + "%" );
	}
}
